package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.BoardMapper;
import com.haiyu.manager.dto.BoardDTO;
import com.haiyu.manager.dto.BoardSearchDTO;
import com.haiyu.manager.pojo.Board;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.BoardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BoardServiceImpl implements BoardService {
    @Autowired
    BoardMapper boardMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public PageDataResult getBoardList(BoardSearchDTO boardSearch, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<BoardDTO> boards = boardMapper.getBoardList(boardSearch);

        System.out.println("board有" + boards);
        PageHelper.startPage(pageNum, pageSize);

        if (boards.size() != 0) {
            PageInfo<BoardDTO> pageInfo = new PageInfo<>(boards);
            pageDataResult.setList(boards);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> updateBoard(Board board) {
        Map<String,Object> data = new HashMap();

        int result = boardMapper.updateBoard(board);
        if(result == 0){
            data.put("code",0);
            data.put("msg","回复失败！");
            logger.error("board[更新]，结果=更新失败！");
            return data;
        }
        data.put("code",1);
        data.put("msg","回复成功！");
        logger.info("board[更新]，结果=更新成功！");
        return data;
    }

    @Override
    public Map<String, Object> delBoard(Integer boardId, Integer boardStatus) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除用户
            int result = boardMapper.updateBoardStatus(boardId,boardStatus);
            if(result == 0){
                data.put("code",0);
                data.put("msg","留言设置为已读失败");
                logger.error("留言设置为已读失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","留言设置为已读成功");
            logger.info("留言设置为已读成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("留言设置为已读异常！", e);
        }
        return data;
    }

    @Override
    public Board getBoardById(Integer boardId) {
        return boardMapper.selectByPrimaryKey(boardId);
    }

    @Override
    public Map<String, Object> recoverBoard(Integer boardId, Integer boardStatus) {
        Map<String, Object> data = new HashMap<>();
        try {
            int result = boardMapper.updateBoardStatus(boardId,boardStatus);
            if(result == 0){
                data.put("code",0);
                data.put("msg","留言设置为未读失败");
            }
            data.put("code",1);
            data.put("msg","留言设置为未读成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("留言设置为未读异常！", e);
        }
        return data;
    }

}
